

#include <uguiscreen.h>
#include <QDebug>
#include <sys/unistd.h>
#include <QPixmap>
#include <debuglog.h>
#include <QMessageBox>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>
#include <QTextCodec>
#include <QByteArray>
#include <QMap>
#include <QList>

#include <fcntl.h>
#include <unistd.h>

#define BUFFSIZE  32

static char buff_send[BUFFSIZE] ={1};
static char buff_read[BUFFSIZE] ={0};

//void draw_polygon(PFBDEV pFbdev, uint32_t num, PPOINT array, void *color)
//{
//        int i;
//        for(i=0; i<num; i++)
//        {
//                draw_line(pFbdev, array[i], array[(i+1)%num], color);
//        }
//}

//void draw_triangle(PFBDEV pFbdev, POINT p1, POINT p2, POINT p3, void *color)
//{
//        POINT p[3];
//        p[0] = p1;
//        p[1] = p2;
//        p[2] = p3;
//        draw_polygon(pFbdev, 3, p, color);
//}

//void draw_circle(PFBDEV pFbdev, POINT center, uint32_t radius, void *color)
//{
//        POINT p;
//        int tmp;
//        for(p.x = center.x - radius; p.x <= center.x + radius; p.x++)
//        {
//                tmp = sqrt(radius * radius - (p.x - center.x) * (p.x - center.x));
//                p.y = center.y + tmp;
//                draw_dot(pFbdev, p, color);
//                p.y = center.y - tmp;
//                draw_dot(pFbdev, p, color);
//        }
//        for(p.y = center.y - radius; p.y <= center.y + radius; p.y++)
//        {
//                tmp = sqrt(radius * radius - (p.y - center.y) * (p.y - center.y));
//                p.x = center.x + tmp;
//                draw_dot(pFbdev, p, color);
//                p.x = center.x - tmp;
//                draw_dot(pFbdev, p, color);
//        }
//}

//void draw_parabola_x(PFBDEV pFbdev, POINT center, int a, void *color)
//{
//        POINT p;
//        for(p.x = center.x - 100; p.x < center.x + 100; p.x++)
//        {
//                p.y = (p.x - center.x) * (p.x - center.x) / a + center.y;
//                draw_dot(pFbdev, p, color);
//        }
//}

//void draw_parabola_y(PFBDEV pFbdev, POINT center, int a, void *color)
//{
//        POINT p;
//        for(p.y = center.y - 100; p.y < center.y + 100; p.y++)
//        {
//                p.x = (p.y - center.y) * (p.y-center.y) / a + center.x;
//                draw_dot(pFbdev, p, color);
//        }
//}

//void rectangle_fill(PFBDEV pFbdev, POINT p1, POINT p2, void *color)
//{
//        POINT p_t1, p_t2;
//    int y;

//    p_t1.x = MIN(p1.x, p2.x);
//    p_t2.x = MAX(p1.x, p2.x);

//    for(y = MIN(p1.y, p2.y); y <= MAX(p1.y, p2.y); y++){
//        p_t1.y = p_t2.y = y;
//        draw_line(pFbdev, p_t1, p_t2, color);
//    }
//}

//void rgb_test(PFBDEV pFbdev)
//{
//        POINT p, p1, p2;
//        uint32_t color;

//        p1.x = 0;
//        p1.y = 0;
//        p2.x = pFbdev->fb_var.xres - 1;
//        p2.y = (pFbdev->fb_var.yres / 3) - 1;
//        color = (pFbdev->fb_var.bits_per_pixel == 16) ? RED16 : RED24;
//        rectangle_fill(pFbdev, p1, p2, (void *)&color);

//    p1.x = 0;
//        p1.y = pFbdev->fb_var.yres / 3;
//        p2.x = pFbdev->fb_var.xres - 1;
//        p2.y = (pFbdev->fb_var.yres / 3 * 2) - 1;
//    color = (pFbdev->fb_var.bits_per_pixel == 16) ? GREEN16 : GREEN24;
//        rectangle_fill(pFbdev, p1, p2, (void *)&color);

//    p1.x = 0;
//        p1.y = (pFbdev->fb_var.yres / 3 * 2);
//        p2.x = pFbdev->fb_var.xres - 1;
//        p2.y = pFbdev->fb_var.yres - 1;
//        color = (pFbdev->fb_var.bits_per_pixel == 16) ? BLUE16 : BLUE24;
//        rectangle_fill(pFbdev, p1, p2, (void *)&color);
//}

//void gray_test(PFBDEV pFbdev)
//{
//    POINT p, p1, p2;
//        uint8_t x_step, y_step;
//    uint32_t gray[16][16];
//        int i, j;
//    int GRAY_NUM, ROW_NUM, COL_NUM;

//    GRAY_NUM = (pFbdev->fb_var.bits_per_pixel == 16) ? GRAY_16 : GRAY_24;
//    ROW_NUM = (pFbdev->fb_var.bits_per_pixel == 16) ? ROW_16 : ROW_24;
//    COL_NUM = (pFbdev->fb_var.bits_per_pixel == 16) ? COL_16 : COL_24;

//    x_step = pFbdev->fb_var.xres / COL_NUM;
//    y_step = pFbdev->fb_var.yres / ROW_NUM;

//        for(i = 0; i< GRAY_NUM; i++){
//                gray[i/ROW_NUM][i%COL_NUM] = (pFbdev->fb_var.bits_per_pixel == 16) ?
//                        (i << 11 | i << 6 | i) : (i << 16 | i << 8 | i);
//    }

//        for(j = 0; j < ROW_NUM; j++){
//                for(i = 0; i < COL_NUM; i++){
//                        p1.x = i * x_step;
//                        p2.x = p1.x + x_step - 1;
//                        p1.y = j * y_step;
//                        p2.y = p1.y + y_step - 1;
//                        rectangle_fill(pFbdev, p1, p2, (void *)&gray[j][i]);
//                }
//        }
//}

//uint32_t color_get(PFBDEV pFbdev, int x)
//{
//        int Gx =  pFbdev->fb_var.xres / 2;
//        int Rx = Gx - (pFbdev->fb_var.xres / 3);
//        int Bx = Gx + (pFbdev->fb_var.xres / 3);
//    int Dmax = pFbdev->fb_var.xres / 3;
//    int R, G, B;
//    int R_MAX, G_MAX, B_MAX;
//    int dx;

//    R_MAX = (pFbdev->fb_var.bits_per_pixel == 16) ? R_MAX16 : R_MAX24;
//    G_MAX = (pFbdev->fb_var.bits_per_pixel == 16) ? G_MAX16 : G_MAX24;
//    B_MAX = (pFbdev->fb_var.bits_per_pixel == 16) ? B_MAX16 : B_MAX24;

//    if(x > Bx){
//        dx = (Dmax / 2) + (pFbdev->fb_var.xres - x);
//    }else{
//            dx = abs(x - Rx);
//            dx = (dx < Dmax) ? dx : Dmax;
//    }
//        R = R_MAX - (R_MAX * dx / Dmax);
//    R = (R < R_MAX) ? R : (R_MAX -1);

//        dx = abs(x - Gx);
//        dx = (dx < Dmax) ? dx : Dmax;
//        G = G_MAX - (G_MAX * dx / Dmax);
//    G = (G < G_MAX) ? G : (G_MAX - 1);

//    if(x < Rx){
//        dx = (Dmax / 2) + x;
//    }else{
//            dx = abs(x - Bx);
//            dx = (dx < Dmax) ? dx : Dmax;
//    }
//        B = B_MAX - (B_MAX * dx / Dmax);
//    B = (B < B_MAX) ? B : (B_MAX - 1);


////	printf("x: %d, R: 0x%x, G: 0x%x, B: 0x%x\n", x, R, G, B);

//        return((pFbdev->fb_var.bits_per_pixel == 16) ?
//                        (R << 11 | G << 5 | B) : (R << 16 | G << 8 | B));
//}

//void palette_test(PFBDEV pFbdev)
//{
//    POINT p, p1, p2;
//    uint32_t color;
//    int y;

////	p1.y = 0;
////	p2.y = pFbdev->fb_var.yres - 1;

//    p1.x = 0;
//    p2.x = 479;

//    for(y = 0; y < 100; y++){
//        p1.y = p2.y = y;
////		color = color_get(pFbdev, x);
//        color = 0xf800;
//        draw_line(pFbdev, p1, p2, (void *)&color);
//    }

//    for(y = 100; y < 200; y++){
//                p1.y = p2.y = y;
////              color = color_get(pFbdev, x);
//                color = 0x07e0;
//                draw_line(pFbdev, p1, p2, (void *)&color);
//        }

//    for(y = 200; y < 272; y++){
//                p1.y = p2.y = y;
////              color = color_get(pFbdev, x);
//                color = 0x001f;
//                draw_line(pFbdev, p1, p2, (void *)&color);
//        }


//}




//}

////int main(int argc, char *argv[])
////{
////        FBDEV fbdev;
////    int i;
////        memset(&fbdev, 0, sizeof(FBDEV));
////        strcpy(fbdev.dev, "/dev/fb1");
////        if(fb_open(&fbdev)==FALSE)
////        {
////                printf("open frame buffer error\n");
////                return 0;
////        }

//////	printf("fb_open success\n");

////    palette_test(&fbdev);
////    sleep(300);
////    for(i = 0; i < 50 ; i++)
////    {
////        palette_test_2(&fbdev);
////        sleep(30);
////    }

////    while(1);
////        fb_close(&fbdev);
////        return 0;
////}

/* Some defines */
#define MAX_OBJECTS        10

/* Window 1 */
UG_WINDOW window_1;
UG_OBJECT obj_buff_wnd_1[MAX_OBJECTS];
UG_BUTTON button1_1;
UG_BUTTON button1_2;
UG_BUTTON button1_3;
UG_BUTTON button1_4;
UG_BUTTON button1_5;
UG_BUTTON button1_6;
UG_IMAGE image_1;
void window_1_callback(UG_MESSAGE *msg)
{
    // . . .
}

int SecondGUI_Test(void)
{
    QTextCodec *codec = QTextCodec::codecForName("GBK");


    int i=0;
        int fd0;
        char *buff_send = malloc(50);

        if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
            printf("export is error\r\n");
            return 1;
        }

    //	char *value="5.01";

        buff_send[0] = 'a';
        float value=5.11;
        snprintf(buff_send+1,5,"%f",value);

        write(fd0,buff_send,strlen(buff_send));


        buff_send[0] = 'b';
        char * buf1;

        QByteArray encodedString = codec->fromUnicode("鱼香肉丝");
        buf1 = encodedString.data();

        for(i=0;i<strlen(buf1);i++)
            *(buff_send +i+1)= *(buf1+i);

        write(fd0,buff_send,strlen(buff_send));

        buff_send[0] = 'c';
        buf1 = "红烧肉";
        for(i=0;i<strlen(buf1);i++)
                    *(buff_send +i+1)= *(buf1+i);


        write(fd0,buff_send,strlen(buff_send));

        buff_send[0] = 'd';
        buf1 = "凉拌黄瓜 3元";
        for(i=0;i<strlen(buf1);i++)
                    *(buff_send +i+1)= *(buf1+i);

        write(fd0,buff_send,strlen(buff_send));

        buff_send[0] = 'e';
        buf1 = "大锅菜  10元";
        for(i=0;i<strlen(buf1);i++)
                    *(buff_send +i+1)= *(buf1+i);


        write(fd0,buff_send,strlen(buff_send));

        buff_send[0] = 'f';
        buf1 = "酸菜鱼  20元";
        for(i=0;i<strlen(buf1);i++)
                    *(buff_send +i+1)= *(buf1+i);


        write(fd0,buff_send,strlen(buff_send));

//    sleep(1);
//    char *value2="\r\n积水";
//    write(fd0,value2,strlen(value2));
//    sleep(1);
//    char *value3="\r\n3.33";
//    write(fd0,value3,strlen(value3));


//    FBDEV gfbdev;
//    int i;
//    UG_GUI s_UGUI;
//    UG_WINDOW s_wnd;
//    UG_OBJECT s_ug_obj;

//    char c_temp_buffer[32];
//    UG_Init(&gfbdev,&s_UGUI,pset_draw_dot,480,272);
//    UG_FontSelect (&FONT_24X40 );
//    memset(&gfbdev, 0, sizeof(FBDEV));
//    strcpy(gfbdev.dev, "/dev/fb1");
//    if(fb_open(&gfbdev)==0)
//    {
//        printf("open f/rame buffer error\n");
//         qDebug() << "open frame buffer error : ";
//        return 0;
//    }
//    qDebug() << "open frame buffer ok : ";

//       UG_FillScreen(0);
//        sleep(10);
//        UG_FillScreen( 0 );

//        UG_WindowCreate(&window_1 ,obj_buff_wnd_1 , MAX_OBJECTS, window_1_callback) ;
//        UG_WindowSetForeColor(&window_1,0x0000AA);
//        UG_WindowSetTitleText( &window_1, "epos" );
//        UG_WindowSetTitleTextFont( &window_1, &FONT_12X20 );
//        UG_ButtonCreate(&window_1,&button1_1,BTN_ID_0,10,10,110,60);
//        UG_ButtonCreate(&window_1,&button1_2,BTN_ID_1,10,80,110,130);
//        UG_ButtonCreate(&window_1,&button1_3,BTN_ID_2,10,150,110,200);
     //   UGButtonCreate(&window1,&button1_4,BTN_ID_0,10,10,110,60);

        /* Configure Button 1 */
//        UG_ButtonSetFont( &window_1, BTN_ID_0, &FONT_12X20 );
//        UG_ButtonSetBackColor( &window_1, BTN_ID_0, C_LIME );
//        UG_ButtonSetText( &window_1, BTN_ID_0, "G " );
//        /* Configure Button 2 */
//        UG_ButtonSetFont( &window_1, BTN_ID_1, &FONT_12X20 );
//        UG_ButtonSetBackColor( &window_1, BTN_ID_1, C_RED );
//        UG_ButtonSetText( &window_1, BTN_ID_1, "R " );
//        /* Configure Button 3 */
//        UG_ButtonSetFont( &window_1, BTN_ID_2, &FONT_12X20 );
//        UG_ButtonSetText( &window_1, BTN_ID_2, "2" );
//        UG_WindowShow( &window_1 );
//        UG_Update();
//        sleep(5);

//        QPixmap pix;
//        pix.load("second_paying.png");
//        pix.load("222.png");
//        QImage img = pix.toImage();
//        img = img.convertToFormat(QImage::Format_RGB888);

//        int width = img.width();
//        int height = img.height();

//        QString str1;
//        str1.setNum(width);
//        QString str2;
//        str2.setNum(height);

        //QMessageBox::about(NULL, "image", "width:" + str1 + ";height:" + str2);
//        int line = (width * 24 + 31) / 8;
//        UG_U16 bmpData[line * height];

//        unsigned char *data = img.bits();
//        unsigned char a, r, g, b;
//        for(int i = 0; i < height; i++)
//        {
//            for(int j = 0; j < width; j++)
//            {
//                a = *(data + 3);
//                r = *(data + 2);
//                g = *(data + 1);
//                b = *data;

//                bmpData[i * line + j * 3] = b;
//                bmpData[i * line + j * 3 + 1] = g;
//                bmpData[i * line + j * 3 + 2] = r;
//                bmpData[i * line + j * 3 + 3] = a;

//                data += 4;
//            }
//        }

//        const UG_U16 logo_bmp[] = { 23, 32, 11, 24};
//        const UG_BMP logo = {
//            (void*)bmpData,
//            width,
//            height,
//            BMP_BPP_16,
//            BMP_RGB565
//        };

        //UG_WindowCreate (&window_1 , obj_buff_wnd_1 , MAX_OBJECTS, window_1_callback);

        //UG_ImageCreate(&window_1, &image_1, IMG_ID_0, 0, 0, width, width);
        //UG_ImageSetBMP(&window_1, IMG_ID_0, &logo) ;
        //UG_WindowShow(&window_1) ;
        //UG_Update();

//        for(int i = 0; i < width; i++)
//        {
//            for(int j = 0; j < height; j++)
//            {
//                UG_DrawPixel(i, j, img.pixel(i, j));
//            }
//        }

        //for(int i=0;i<10;i++)
        //{
          //  sleep(1);
            //sprintf(c_temp_buffer,"%d",i);
            //UG_PutChar(0x30+i,100+i*s_UGUI.font.char_width,100,0xBF3EFF,0xB0E2FF);
          //  UG_ConsolePutString(c_temp_buffer);
            //UG_ButtonSetText( &window_1, BTN_ID_2, c_temp_buffer );
            // UG_Update();
              //sleep(30);
            //usleep(300000);
//               qDebug() << "UG_put char "<<i;
        //}
  //  UG_WaitForUpdate();

//         qDebug() << "UG_WindowShow ";
//        sleep(10);
//        UG_FillCircle(50,50,20,0x333);
//        qDebug() << "Circle 2 ";
//         sleep(1);
//        //UG_DrawFrame(10,10,100,100,0x3300);
//    for(int i = 0; i < 10; i++)
//    {
//          UG_DrawLine(100+i*10,0,100+i*10,150,0x3399);
//    }

//        usleep(1);
}

void WriteText(QString text){
    int i=0;
    int fd0;
    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
        printf("export is error\r\n");
        return 1;
    }

    char *str2  = "          ";
    char buff_send2[16];
    buff_send2[0] = 'a';
    sprintf(buff_send2+1, str2);
    write(fd0,buff_send2,strlen(buff_send2));

    char buff_send[16];
    buff_send[0] = 'a';
    QByteArray ba = text.toLatin1();
    char *str = ba.data();
    sprintf(buff_send+1, str);
    write(fd0,buff_send,strlen(buff_send) + 1);

    close(fd0);

}
void WriteText2(QString text){
    int i=0;
    int fd0;
    char buff_send[50];

    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
        printf("export is error\r\n");
        return 1;
    }

    buff_send[0] = 'a';
    sprintf(buff_send+1,"         ");
    write(fd0,buff_send,strlen(buff_send));

    close(fd0);
}

void WriteLine(QString name, QString price, int index){

    QByteArray string1 = name.toLatin1();
    char *data1 = string1.data();
    QByteArray string2 = price.toLatin1();
    char *data2 = string2.data();
    printf("name: %s \r\n", data1);
    printf("price: %s \r\n", data2);
    printf("index: %d \r\n", index);

    char lines[5];
    lines[0] = 'b';
    lines[1] = 'c';
    lines[2] = 'd';
    lines[3] = 'e';
    lines[4] = 'f';

//    int i=0;
//    int fd0;
//    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
//        printf("export is error\r\n");
//        return 1;
//    }

//    char buff_send[100];
//    buff_send[0] = lines[index];

//    QTextCodec *codec = QTextCodec::codecForName("GBK");
//    QByteArray string = codec->fromUnicode(name);
//    char *str = string.data();
//    //sprintf(buff_send+1, str);

//    for(i=0;i<strlen(str);i++)
//        *(buff_send +i+1)= *(str+i);

//    write(fd0,buff_send,strlen(buff_send) + 1);

//    close(fd0);


    int i=0;
    int fd0;
    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
        printf("export is error\r\n");
        return 1;
    }

    char *str2  = "                                    ";
    char buff_send2[100];
    buff_send2[0] = lines[index];
    sprintf(buff_send2+1, str2);
    write(fd0,buff_send2,strlen(buff_send2));

    char buff_send[100];
    buff_send[0] = lines[index];
    QTextCodec *codec = QTextCodec::codecForName("GBK");
    QByteArray string = codec->fromUnicode(name);
    char *str = string.data();
    sprintf(buff_send+1, str);
    printf("len: %d \r\n", strlen(buff_send));
    write(fd0,buff_send,strlen(buff_send) + 1);

    printf("closing\r\n");
    close(fd0);
    printf("closed\r\n");

}

void WriteQMap(QMap<QString, QString> tabList){

    char lines[5];
    lines[0] = 'b';
    lines[1] = 'c';
    lines[2] = 'd';
    lines[3] = 'e';
    lines[4] = 'f';

    int i=0;
    int fd0;
    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
        printf("export is error\r\n");
        return 1;
    }

    QList<QString> names = tabList.keys();
    if(names.length() > 0)
    {
        for(int i = (names.length() - 1); i >= 0; i--)
        {
            QString dataline = tabList.value(names.at(i));
            QStringList list = dataline.split(',');
            if(list.count() == 2){
                QString price = list.at(0);
                QString name = names.at(i);
                int index = names.length() - 1 - i;
                if(index > 4) return 1;

                char *str2  = "                                                      ";
                char buff_send2[100];
                buff_send2[0] = lines[index];
                sprintf(buff_send2+1, str2);
                printf("   %s\r\n",buff_send2);

                write(fd0,buff_send2,strlen(buff_send2));

                char buff_send[100];
                buff_send[0] = lines[index];

                QTextCodec *codec = QTextCodec::codecForName("GBK");
                QByteArray string = codec->fromUnicode(name);
                char *str = string.data();
                sprintf(buff_send+1, str);
                printf(" %s\r\n",buff_send);
                write(fd0,buff_send,strlen(buff_send) + 1);

            }

        }
    }
    sleep(1);
    printf("closing open \r\n");
////    if(close(fd0) == 0)
////    {
////        printf("close ok\r\n");

////    }
//    else
//        printf("close error \r\n");
    return;

}

void Clear(){

    int i=0;
    int fd0;
    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
        printf("export is error\r\n");
        return 1;
    }

    char *str2  = "                                                      ";
    char buff_send1[100];
    buff_send1[0] = 'b';
    sprintf(buff_send1+1, str2);
    write(fd0,buff_send1,strlen(buff_send1));

    char buff_send2[100];
    buff_send2[0] = 'c';
    sprintf(buff_send2+1, str2);
    write(fd0,buff_send2,strlen(buff_send2));

    char buff_send3[100];
    buff_send3[0] = 'd';
    sprintf(buff_send3+1, str2);
    write(fd0,buff_send3,strlen(buff_send3));

    char buff_send4[100];
    buff_send4[0] = 'e';
    sprintf(buff_send4+1, str2);
    write(fd0,buff_send4,strlen(buff_send4));

    char buff_send5[100];
    buff_send5[0] = 'f';
    sprintf(buff_send5+1, str2);
    write(fd0,buff_send5,strlen(buff_send5));

    close(fd0);

}

void TestWrite(){

    int i=0;
    int fd0;
    char *str2  = "         ";

    if ((fd0 = open("/dev/lcd_spi", O_RDWR)) < 0) {
        printf("export is error\r\n");
        return 1;
    }


    char buff_send[16];
    buff_send[0] = 'a';
    sprintf(buff_send+1, "48.50");
    write(fd0,buff_send,strlen(buff_send) + 1);

    printf(" %d \r\n",strlen(buff_send));

    sleep(3);

    char buff_send2[16];
    buff_send2[0] = 'a';
    sprintf(buff_send2+1, str2);
    write(fd0,buff_send2,strlen(buff_send2));

    sleep(3);

    char buff_send3[16];
    buff_send3[0] = 'a';
    sprintf(buff_send3+1, "12.50");
    write(fd0,buff_send3,strlen(buff_send3) + 1);

    sleep(3);

    char buff_send4[16];
    buff_send4[0] = 'a';
    sprintf(buff_send4+1, str2);
    write(fd0,buff_send4,strlen(buff_send4));

    sleep(3);

    char buff_send5[16];
    buff_send5[0] = 'a';
    sprintf(buff_send5+1, "5.5");
    write(fd0,buff_send5,strlen(buff_send5) + 1);

}
